package service.impl;

import java.util.List;

import org.junit.Test;

import com.opensymphony.xwork2.util.ResolverUtil.NameEndsWith;

import dao.DepartmentDao;
import entity.Department;
import entity.PageBean;
import service.DepartmentService;

public class DepartmentServiceImpl implements DepartmentService {

	// 注入dao
	private DepartmentDao departmentDao;

	public DepartmentDao getDepartmentDao() {
		return departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public List<Department> queryAll() {
		return departmentDao.queryAll();
	}

	/*
	 * 分页查询的业务逻辑方法 (non-Javadoc)
	 * 
	 * @see service.DepartmentService#findByPage(int)
	 */
	@Test
	public PageBean<Department> findByPage(int currPage) {
		PageBean<Department> pageBean = new PageBean<Department>();
		pageBean.setCurrPage(currPage);
		int pageSize = 3;
		pageBean.setPageSize(pageSize);// 每页显示的记录数
		int totalCount = departmentDao.calCount();
		pageBean.setTotalCount(totalCount);
		double tc = totalCount;
		Double totalPage = Math.ceil(tc / pageSize);
		pageBean.setTotalPage(totalPage.intValue());
		int begin = (currPage - 1) * pageSize;
		List<Department> list = departmentDao.findByPage(begin, pageSize);
		pageBean.setList(list);
	
		return pageBean;
	}

	public void save(Department department) {
		departmentDao.save(department);
	}

	public Department findById(Integer did) {
		return departmentDao.findById(did);
	}

	public void update(Department department) {
		departmentDao.update(department);
	}

	public void delete(Department department) {
		departmentDao.delete(department);
	}

	public List<Department> findAll() {
		return departmentDao.findAll();
	}

}
